package com.yaoyanshe.trialfield.common.utils;

import android.content.Context;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import android.widget.Toast;

import androidx.core.content.ContextCompat;

import com.yaoyanshe.trialfield.common.R;


/**
 * author: tck
 * created on: 2018/8/9 17:02
 * description:
 */
public class ToastUtils {

    private static Toast mToast = null;

    public static void showToast(Context context, String text) {
        if (mToast != null) {
            mToast.cancel();
        }
        mToast = Toast.makeText(context.getApplicationContext(), text + "", Toast.LENGTH_SHORT);
        mToast.show();
    }

    public static void showCenterToast(Context context, String text) {
        if (mToast != null) {
            mToast.cancel();
        }
        Context applicationContext = context.getApplicationContext();
        FrameLayout frameLayout = new FrameLayout(applicationContext);
        frameLayout.setMinimumWidth(ScreenUtils.dp2px(125f));
        frameLayout.setMinimumHeight(ScreenUtils.dp2px(40f));
        frameLayout.setLayoutParams(new ViewGroup.LayoutParams(-2, -2));
        frameLayout.setBackground(ContextCompat.getDrawable(applicationContext, R.drawable.shape_corners_4dp_solid_b3000000));
        TextView textView = new TextView(applicationContext);
        textView.setText(text);
        textView.setTextSize(12);
        textView.setTextColor(0xffffffff);
        FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(-2, -2, Gravity.CENTER);
        int size10 = ScreenUtils.dp2px(applicationContext, 10f);
        layoutParams.leftMargin = size10;
        layoutParams.rightMargin = size10;
        frameLayout.addView(textView, layoutParams);
        mToast = new Toast(applicationContext);
        mToast.setDuration(Toast.LENGTH_SHORT);
        mToast.setView(frameLayout);
        mToast.setGravity(Gravity.CENTER, 0, 0);
        mToast.show();
    }

    public static void cancelToast() {
        if (mToast != null) {
            mToast.cancel();
        }
    }

}
